package org.crystalrange.files.web;

import java.util.List;
import java.util.Set;

import org.crystalrange.BaseController;
import org.crystalrange.files.model.FileType;
import org.crystalrange.files.service.FileTypeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.googlecode.genericdao.search.Search;

public class FileTypeListController extends BaseController {

	protected FileType selectedFileType;
	protected FileTypeService fileTypeService;
	protected List<FileType> fileTypes;
	protected Listbox listboxFileType;
	protected Search searchFileType;

	public FileType getSelectedFileType() {
		return selectedFileType;
	}

	public void setSelectedFileType(FileType selectedFileType) {
		this.selectedFileType = selectedFileType;
	}

	public FileTypeService getFileTypeService() {
		return fileTypeService;
	}

	public void setFileTypeService(FileTypeService fileTypeService) {
		this.fileTypeService = fileTypeService;
	}

	public List<FileType> getFileTypes() {
		return fileTypes;
	}

	public void setFileTypes(List<FileType> fileTypes) {
		this.fileTypes = fileTypes;
	}

	public void onClick$toolbarbuttonFileTypeCreate(Event event)
			throws Exception {
		this.fileTypeCreate();
	}

	public void onClick$menuitemFileTypeCreate(Event event) throws Exception {
		this.fileTypeCreate();
	}

	public void onClick$toolbarbuttonFileTypeEdit(Event event) throws Exception {
		this.fileTypeEdit();
	}

	public void onClick$menuitemFileTypeEdit(Event event) throws Exception {
		this.fileTypeEdit();
	}

	public void onClick$toolbarbuttonFileTypeDelete(Event event)
			throws Exception {
		this.fileTypeDelete();
	}

	public void onClick$menuitemFileTypeDelete(Event event) throws Exception {
		this.fileTypeDelete();
	}

	public void onClick$toolbarbuttonFileTypeSearch(Event event)
			throws Exception {
		this.fileTypeSearch();
	}

	public void onClick$menuitemFileTypeSearch(Event event) throws Exception {
		this.fileTypeSearch();
	}

	public void onClick$toolbarbuttonFileTypeRefresh(Event event)
			throws Exception {
		this.fileTypeRefresh();
	}

	public void onClick$menuitemFileTypeRefresh(Event event) throws Exception {
		this.fileTypeRefresh();
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.searchFileType = new Search(FileType.class);
		this.searchFileType.setMaxResults(100);
		this.searchFileType.addFetch("createdBy");
		this.fileTypes = this.fileTypeService.search(this.searchFileType);
		component.setAttribute("controller", this);
	}

	private void fileTypeCreate() throws Exception {
		this.execution.sendRedirect("~./zul/html/fileType.zul");
	}

	private void fileTypeEdit() throws Exception {
		if (this.selectedFileType != null) {
			this.execution.sendRedirect("~./zul/html/fileType.zul?Id="
					+ this.selectedFileType.getId().toString());
		}
	}

	private void fileTypeDelete() throws Exception {
		Set<Listitem> selectedItems = this.listboxFileType.getSelectedItems();

		if (selectedItems.size() > 0) {
			if (Messagebox.YES == Messagebox.show("Delete these records?",
					"Delete?", Messagebox.YES | Messagebox.NO,
					Messagebox.QUESTION)) {
				for (Listitem selectedItem : selectedItems) {
					FileType fileType = (FileType) selectedItem.getValue();
					this.fileTypeService.remove(fileType);
				}
				this.selectedFileType = null;
			}
		} else {
			this.message.setText("Please select at least one item!");
			this.message.setIcon("/img/exclamation.png");
		}
		this.fileTypes = this.fileTypeService.search(this.searchFileType);
		this.binder.loadAll();
	}

	private void fileTypeSearch() throws Exception {
		this.execution.setAttribute("searchFileType", this.searchFileType);

		Window window = (Window) this.execution.createComponents(
				"~./zul/html/fileTypeSearch.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			this.searchFileType = (Search) this.execution
					.getAttribute("searchFileType");
		}

		this.fileTypes = this.fileTypeService.search(this.searchFileType);
		this.binder.loadAll();
	}

	private void fileTypeRefresh() throws Exception {
		this.fileTypes = this.fileTypeService.search(this.searchFileType);
		this.binder.loadAll();
	}

}
